package com.waji.http;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;

import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVFile;
import com.avos.avoscloud.AVGeoPoint;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.SaveCallback;
import com.waji.local.SharedPrefer;
import com.waji.utils.ByteBitmap;
import com.waji.utils.StreamUtils;

public class SendImage {

	public static String path1;
	public static String path2;
	String fileName1;
	String fileName2;
	byte[] data1 = null,data2 = null;

	public void send(final Context context, final int who) {
		int number1 = SharedPrefer.ReadNumber(context);
		if (who == 1) {
			fileName1 = "1.jpg";
			fileName2 = "2.jpg";
			number1 = number1 + 1;
		}
		if (who == 2) {
			fileName1 = path1;
			fileName2 = path2;
			System.out.println("服务照片地址" + path1 + "......" + path2);
		}

		String address = SharedPrefer.ReadAddress(context);
		String number = number1 + "";

		String dir = Environment.getExternalStorageDirectory()
				+ "/Camera/"
				+ new SimpleDateFormat("yyyy—MM—dd").format(new Date()) .toString() + "/" + number + "/";
		try {
			Bitmap bm1 = BitmapFactory.decodeFile(dir + fileName1);
			Bitmap bm2 = BitmapFactory.decodeFile(dir + fileName2);

			ByteBitmap byt = new ByteBitmap();
			  data1 = byt.Bitmap2Bytes(bm1);
			  data2 = byt.Bitmap2Bytes(bm2);
		} catch (Exception e) {
			e.printStackTrace();
		}
		String battery=SharedPrefer.ReadBattery(context);
		String id = SharedPrefer.ReadUsername(context);
		 double longitude = SharedPrefer.ReadLongitude(context);
		double latitude = SharedPrefer.ReadLatitude(context);

		System.out.println("视频转化1" + data1);
		System.out.println("视频转化2" + data2);

		AVObject image = new AVObject("Image");
		AVGeoPoint point = new AVGeoPoint(latitude, longitude);
		image.put("id", id);
		image.put("number", number);
		image.put("battery", battery);
		image.put("address", address);
		image.put("whereCreated", point);
		image.put("image1", new AVFile("picture1", data1));
		image.put("image2", new AVFile("picture2", data2));
		image.saveInBackground(new SaveCallback() {
			@Override
			public void done(AVException e) {
				if (e == null) {
					if (who == 1) {
						Toast.makeText(context, "计数成功", 1).show();
					}
				} else {
					Toast.makeText(context, "保存图片失败，请检查网络是否连接", 1).show();
				}
			}
		});
	}

}
